<script src="__JS__/require.min.js"></script>

<script>
  require.config({
    baseUrl: "__JS__",
    paths: {
      "jquery": "__JS__/jquery.min",
      "pjax": "__JS__/jquery.pjax",
      "vue": "__JS__/vue-2.5.13",
      "moment": "__JS__/moment.min",
      "html5shiv": "__JS__/html5shiv.min",
      "respond": "__JS__/respond.min",
      "jstree": "__JS__/jstree/jstree",
      "jstree.contextmenu": "__JS__/jstree/jstree.contextmenu",
      "jstree.checkbox": "__JS__/jstree/jstree.checkbox",
      "jstree.dnd": "__JS__/jstree/jstree.dnd",
      "layer": "__JS__/layer/layer",
      "layer_config": "__JS__/layer/layer_config",
      "ajaxForm": "__JS__/jquery.form.min",
      "ueditor": "__STATIC__/ueditor/ueditor.all.min",
      "ueditor.parse": "__STATIC__/ueditor/ueditor.parse.min",
      "bootstrap-datetimepicker": "__STATIC__/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min",
      "jq-datetimepicker": "__JS__/jquery.datetimepicker.full.min",
      "jquery-mousewheel": "__JS__/jquery.mousewheel.min",
      'echarts': "__JS__/echarts",
      //'bsupload': "__JS__/bsupload/fileinput",
      'bsupload': "https://cdn.bootcss.com/bootstrap-fileinput/4.4.8/js/fileinput.min",
      'bsupload_zh': "https://cdn.bootcss.com/bootstrap-fileinput/4.4.8/js/locales/zh.min",
      'plupload': "__JS__/plupload/plupload.full.min",
      'plupload-queue': "__JS__/plupload/jquery.plupload.queue/jquery.plupload.queue.min",
      'easing': "__JS__/jquery.easing.min",
      'select2': "__STATIC__/plugins/select2/select2.full.min",
      'socket.io': "__JS__/socket.io",
      'fullcalendar': "__JS__/fullcalendar/fullcalendar",
      'fullcalendar_cn': "__JS__/fullcalendar/zh-cn",
      'jquery-ui': "__JS__/jquery-ui",
      'datatables.net': "__STATIC__/dataTables/js/jquery.dataTables",
      'datatables.net.bootstrap': "__STATIC__/dataTables/js/dataTables.bootstrap.min",
      'datatables.net-buttons': "__STATIC__/dataTables/buttons/js/dataTables.buttons",
      'datatables.net-buttons-html5': "__STATIC__/dataTables/buttons/js/buttons.html5",
      'datatables.net-buttons-print': "__STATIC__/dataTables/buttons/js/buttons.print",
      'datatables.net-buttons-foundation': "__STATIC__/dataTables/buttons/js/buttons.foundation",
      'datatables.net-buttons-flash': "__STATIC__/dataTables/buttons/js/buttons.flash",
      'datatables.net-buttons-colVis': "__STATIC__/dataTables/buttons/js/buttons.colVis",
      'datatables.net-select': "__STATIC__/dataTables/Select/js/dataTables.select",
      'cropperjs':"__JS__/cropper.min"
    },
    shim: {
      'ueditor': {
        deps: ['__STATIC__/ueditor/third-party/zeroclipboard/ZeroClipboard.min.js', '__STATIC__/ueditor/ueditor.config.js'],
        exports: 'UE',
        init: function (ZeroClipboard) {
          //导出到全局变量，供ueditor使用
          window.ZeroClipboard = ZeroClipboard;
        }
      },
      'ueditor.parse': {
        deps: ['__STATIC__/ueditor/ueditor.config.js'],
        exports: 'UE',
      },
      'layer_config': {
        deps: ['layer'],
        exports: 'layer',
      },
      'bsupload_zh': {
        deps: ['bsupload'],
      },
      'jquery.easing.min': {
        deps: ['jquery'],
      },
      'select2': {
        deps: ['jquery'],
      },
      'fullcalendar': {
        deps: ['moment'],
      },
      'fullcalendar_cn': {
        deps: ['fullcalendar'],
      },
      'datatables.net.bootstrap': {
        deps: ['datatables.net'],
      },
      'datatables.net-buttons': {
        deps: ['datatables.net'],
      },
      'datatables.net-select': {
        deps: ['datatables.net'],
      },
      'datatables.net-buttons-html5': {
        deps: ['datatables.net-buttons'],
      },
      'datatables.net-buttons-print': {
        deps: ['datatables.net-buttons'],
      },
      'datatables.net-buttons-foundation': {
        deps: ['datatables.net-buttons'],
      },
      'datatables.net-buttons-flash': {
        deps: ['datatables.net-buttons'],
      },
      'datatables.net-buttons-colVis': {
        deps: ['datatables.net-buttons'],
      },
    },
  });
</script>

<script src="__JS__/jquery.min.js"></script>
<script src="__JS__/bootstrap.min.js"></script>
<script src="__JS__/adminlte.min.js"></script>


<script>
  var LAYER_HOME_URL = "__JS__/layer/";
  var UEDITOR_HOME_URL = "__STATIC__/ueditor/";
  var UEDITOR_UPLOAD_URL = "__ROOT__/uploads";
  var upload_config = {
    language: 'zh', //设置语言
    allowedPreviewTypes: ['image'],
    allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
    //uploadExtraData:{"id": 1, "fileName":'123.mp3'},
    uploadAsync: true, //默认异步上传
    showUpload: true, //是否显示上传按钮
    showPreview: false, //是否显示预览
    showCaption: true,//是否显示标题
    browseClass: "btn btn-primary", //按钮样式
    dropZoneEnabled: false,//是否显示拖拽区域
    maxFileSize: 10240,//单位为kb，如果为0表示不限制文件大小
    maxFileCount: 1, //表示允许同时上传的最大文件个数
    enctype: 'multipart/form-data',
    validateInitialCount: true,
    previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
    msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
  }

  function init_bs_datetime() {
    var dateTimeInput = $("input.bs-datetime");
    if (dateTimeInput.length > 0) {
      require(["jq-datetimepicker"], function (e) {
        $.datetimepicker.setLocale('zh');
        dateTimeInput.each(function (i, obj) {
          var $obj = $(obj), options = {};
          options.step = $obj.attr('data-step') ? $obj.attr('data-step') : 30;
          options.minDate = $obj.attr('mindate');
          options.maxDate = $obj.attr('maxdate');
          $(obj).datetimepicker(options);
        });
      });
    }
  }
  ;

  function setCookie(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) +
      ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
  }

  //取回cookie
  function getCookie(c_name) {
    if (document.cookie.length > 0) {
      c_start = document.cookie.indexOf(c_name + "=")
      if (c_start != -1) {
        c_start = c_start + c_name.length + 1
        c_end = document.cookie.indexOf(";", c_start)
        if (c_end == -1) c_end = document.cookie.length
        return unescape(document.cookie.substring(c_start, c_end))
      }
    }
    return ""
  }

  function js_alert(result, callback) {
    var msg, href, time = 1000;
    if (typeof (result) == "object") {
      msg = result.msg;
      time = result.wait * time;
      href = result.url;
    } else {
      msg = result;
    }
    //
    layer.alert(msg, {
      icon: 0,
      offset: '150px',
    }, function (index) {
      if (typeof (callback) == "function") {
        callback();
      }

      layer.close(index);
    });
  }

  function js_message(result, reload) {
    var msg, href, time = 1000;
    if (typeof (result) == "object") {
      msg = result.msg;
      time = result.wait * time;
      href = result.url;
    } else {
      msg = result;
    }
    layer.msg(msg, {
      time: time,
      offset: '150px',
      icon: 1,
      area: '30%',
      anim: 2,
      fixed: false,
    }, function () {
      if (reload === true) {
        if (!href)
          window.location.reload();
        else
          window.location.href = href;
      } else if (reload) {
        window.location.href = reload;
      }
    });
  }

  function popup_upload_image(obj, callback) {
    var $this = obj;
    var width = $this.attr("data-width") ? $this.attr("data-width") : "60%";
    var height = $this.attr("data-height") ? $this.attr("data-height") : "60%";
    var is_refresh = false;
    layer.open({
      maxmin: true,
      btn: ['保存', '关闭'],
      btn1: function (index, layero) {
        var body = layer.getChildFrame('body', index)
          , table = body.find('#myTable')
          , checkboxes = table.find("input:checkbox:checked")
          , data = [];
        ;
        checkboxes.each(function (i, obj) {
          var $obj = $(obj);
          data.push({
            filepath: $obj.data("filepath"),
            origin_name: $obj.data("originname"),
            id: parseInt($obj.data("fileid")),
          });
        });
        callback(data);
        layer.close(index)
        return false;
      },
      btn2: function (index, layero) {
        if (is_refresh) parent.location.reload();
      },
      shadeClose: true,
      shade: 0.8,
      offset: "150px",
      type: 2,
      area: [width, height],
      content: ["{:url('base/file/layer')}", 'no'],//不让iframe出现滚动条content: ['http://sentsin.com', 'no']
    });
    return false;
  }

  function popup_user_select(data, callback, isSingle) {
    var href = "{:url('base/user/user_select_layer',false,null)}";
    href += "?";
    if (data && data.classId) {
      href = href + '&filter[classid]=' + data.classId;
    }
    if (data && data.isAll) {
      href = href + '&filter[isall]=' + 1;
    }
    layer.open({
      type: 2,
      offset: "150px",
      area: [data && data.width ? data.width : '70%', data && data.height ? data.height : '70%'],
      content: href,
      btn: ['保存', '关闭'],
      success: function (layero, index) {
        var body = layer.getChildFrame('body', index)
          , iframeWin = window[layero.find('iframe')[0]['name']]
          , checkboxes = body.find('.user_select')
          , divs = body.find('.users')
          , total = 0
          , users = data && data.users ? data.users : []
        ;
        if (isSingle == true) {
          checkboxes.on("change", function () {
            var me = $(this), checked = body.find('.user_select:checked');
            if (checked.length > 1) {
              me.attr('checked', false);
              alert('只能单选');
              return false;
            }
          });
        }
        if (users && users.length > 0) {
          for (var i in users) {
            var obj = body.find(':checkbox[value=' + users[i] + ']');
            obj.prop('checked', true);
            console.log(obj);
            
            //obj[0].dispatchEvent(new Event('change'));
          }
          body.find('.js-user-total').text(total);
        }
      },
      btn1: function (index, layero) {
        var body = layer.getChildFrame('body', index);
        var form = body.find('.users');
        var checked = form.find("input[type=checkbox]:checked");
        var arr = [], $obj;
        checked.each(function (i, obj) {
          $obj = $(obj);
          arr.push({"realname": $obj.parent().text().trim(), "uid": $obj.val(), 'questions': ""});
        });
        //console.log(arr);
        callback(arr);
        layer.close(index);
      },
    });
  }


  $(function () {
    var obj_menu = $("ul.sidebar-menu"), hrefs = obj_menu.find("a"), current_href = window.location.pathname;
    var last_href = getCookie('current_href');
    var current_menu_id = getCookie('current_menu_id');
    var hasMenu=false;
    hrefs.each(function (i, obj) {
      var obj = $(obj), href = obj.attr("href");
      var li = obj.parents("li"), is_hide = li.data('hide');
      //没有子元素的时候将菜单删除
      if (href == "#" && obj.next("ul").find("a").length == 0) {
        obj.remove();
        return true;
      }
      if (last_href == href) {
        obj.parents(".treeview ").addClass("active");
      }
      if (current_href == href) {
        var span = obj.find("span").eq(0);
        span.css("color", "#fff").wrap("<b>");
        //console.log(span.text());
        document.title = "{:config('system.system_name')}-" + span.text();
        obj.parents(".treeview ").addClass("active");
        setCookie('current_href', href);
        setCookie('current_menu_id', li.attr("id"));
        hasMenu=true;
        return true;
      }

      $(".js-menu-level-1").addClass("active").children("a").removeClass('hide');
      //$(".js-menu-level-2").addClass("active");
    });
    if(!hasMenu){
      var menuObj=$("#"+current_menu_id);
      if(menuObj.length==0) return;
      menuObj.find("span").eq(0).css("color", "#fff").wrap("<b>");
    }


  });

  $(function () {
    $.post('{:url("admin/index/resit")}', {}, function (result) {
      var count = result;
      console.log(count);
      $(".js-menu-resit").find(".label").text(count);
    }, 'json');
  });

  $(function () {

    $('[data-toggle="tooltip"]').tooltip();

    // .js-ajax-submit is here
    // will append more form data
    $('body').on("click", 'button.js-ajax-submit', function () {
      var btn = $(this)
        , form = btn.parents('form')
        , message = btn.data('message')
        , options = {offset: "150px"}
        , href = form.attr("action")
      ;
      var ajaxFormOption = {
        type: "post",  //提交方式
        dataType: "json", //数据类型
        //data: myData,//自定义数据参数，视情况添加
        success: function (result) { //提交成功的回调函数
          if (result.code == 0) {
            js_alert(result.msg);
            return false;
          }
          layer.msg(result.msg, {
            time: 1000,
            offset: '150px',
            icon: 1,
            area: '30%',
            anim: 2,
            fixed: false,
          }, function (index) {
            if (result.url) {
              window.location.href = result.url;
            }
            layer.close(index);
            btn.attr("disabled", false);
          });

        }, error: function (result) {
          btn.attr("disabled", false);
          layer.alert('网络错误');
        }
      };
      console.log(ajaxFormOption);

      if (message) {
        var args = form.serialize();
        layer.confirm(message, options, function (index) {
          $.ajax({
            "url": href,
            "type": "post",
            "data": args,
            "success": function (result) {
              if (result.code == 1) {
                js_message(result.msg, true);
              } else {
                js_message(result);
              }
            },
            "error": function (result) {
              js_alert("{:lang('network error')}");
            }
          })
        });
        return false;
      } else {
        form.ajaxForm(ajaxFormOption);
      }
    });
    //
    $("body").on('click', ".js-confirm", function () {
      var me = $(this)
        , href = me.attr('href')
        , message = me.data("message") ? me.data("message") : '确定执行该操作吗？'
        , options = {offset: "150px"}
      ;
      layer.confirm(message, options, function (index) {
        $.ajax({
          "url": href,
          "type": "get",
          "data": {},
          "success": function (result) {
            if (result.code == 1) {
              js_message(result.msg, true);
            } else {
              js_message(result);
            }
          },
          "error": function (result) {
            js_alert("{:lang('network error')}");
          }
        })
      });
      return false;
    });
  })
  ;
</script>




